package array;

import java.util.ArrayList;
import java.util.List;

public class 文件组合 {
    //求和公式
    class Solution {
        public int[][] fileCombination(int target) {
            int i = 1;
            double j = 2.0;
            List<int[]> res = new ArrayList<>();
            while(i < j) {
                j = (-1 + Math.sqrt(1 + 4 * (2 * target + (long) i * i - i))) / 2;
                if(i < j && j == (int)j) {
                    int[] ans = new int[(int)j - i + 1];
                    for(int k = i; k <= (int)j; k++)
                        ans[k - i] = k;
                    res.add(ans);
                }
                i++;
            }
            return res.toArray(new int[0][]);
        }
    }
    //滑动窗口
    class Solution2 {
        public int[][] fileCombination(int target) {
            int i=1,j=2,s=3;
            List<int[]> res = new ArrayList<>();
            while(i<j){
                if(s == target){
                    int[] ans = new int[j-i+1];
                    for(int k = i; k <= j; k++)
                        ans[k - i] = k;
                    res.add(ans);
                }
                if(s < target){
                    j++;
                    s += j;
                } else{
                    s -= i;
                    i++;
                }
            }
            return res.toArray(new int[0][]);
        }
    }
}
